<?php
namespace Services\Sys;

use Helpers\Cache;
use Helpers\Di;
use Helpers\Main;
use Services\Sys\Models\XhsysEnum;
use Services\Sys\Models\XhsysTree;

class Enum
{
    /**
     * 根据名称获取枚举内容
     *
     * @param $enumName
     * @return array|bool|mixed|null
     * @throws \Exception
     *
     * create by ck 20181112
     */
    public static function getEnum(string $enumName)
    {
        $cacheKey = 'enum_name_' . $enumName;
        if (Cache::exists($cacheKey)) {
            return Cache::get($cacheKey);
        }
        $node = XhsysTree::findFirst([
            'conditions' => "sNodeName = '{$enumName}' AND iNodeType = 2 AND bDelete = 0"
        ]);
        if (!$node) {
            throw new \Exception('枚举不存在');
        }
        $enumList = self::getEnumByNodeId($node->iNodeId);
        Cache::save($cacheKey, $enumList);
        return $enumList;
    }

    /**
     * 根据节点id获取枚举
     *
     * @param $nodeId
     * @return array|bool|mixed|null
     *
     * create by ck 20181112
     */
    private static function getEnumByNodeId($nodeId)
    {
        $cacheKey = 'enum_name_' . $nodeId;
        if (Cache::exists($cacheKey)) {
            return Cache::get($cacheKey);
        }
        $enumList = XhsysEnum::find([
            'conditions' => "iNodeId = {$nodeId} AND bDelete = 0",
            'columns' => 'sEnumValue,sEnumName'
        ])->toArray();
        $enumList = array_column($enumList, 'sEnumName', 'sEnumValue');
        Cache::save($cacheKey, $enumList);
        return $enumList;
    }

    /**
     * 增加枚举项
     *
     * @param $enumName
     * @return int
     * @throws \Exception
     *
     * create by ck 20190108
     */
    public static function addEnum($enumName)
    {
        $node = XhsysTree::findFirst("sNodeName = '{$enumName}' AND iNodeType = 2 AND bDelete = 0");
        if ($node) {
            throw new \Exception('该枚举名称已存在');
        }
        $node = new XhsysTree();
        $node->sNodeName = $enumName;
        $node->iNodeType = 2;
        if (!$node->save()) {
            throw new \Exception($node->getError());
        }
        return $node->iNodeId;
    }

    /**
     * 添加枚举项值
     *
     * @param $nodeId
     * @param $name
     * @param $value
     * @throws \Exception
     *
     * create by ck 20190108
     */
    public static function addEnumValue($nodeId, $name, $value)
    {
        $enum = new XhsysEnum();
        $enum->iNodeId    = $nodeId;
        $enum->sEnumName  = $name;
        $enum->sEnumValue = $value;
        if (!$enum->save()) {
            throw new \Exception($enum->getError());
        }
    }

    /**
     * 获取枚举列表
     *
     * @return array
     *
     * create by ck 20190108
     */
    public static function getEnumList()
    {
        $builder = Di::get()->modelsManager->createBuilder()
            ->from(['enum' => '\\Services\\Sys\\Models\\XhsysEnum'])
            ->columns(['enum.iNodeId', 'enum.iEnumId', 'enum.sEnumName', 'enum.sEnumValue', 'tree.sNodeName'])
            ->leftJoin('\\Services\\Sys\\Models\\XhsysTree', 'enum.iNodeId = tree.iNodeId', 'tree')
            ->where('enum.bDelete = 0 AND tree.bDelete = 0')
            ->orderBy('enum.iNodeId,enum.iEnumId');
        return Main::runBuilderPage($builder);
    }

    /**
     * 编辑枚举
     *
     * @param $enumId
     * @param $name
     * @param $value
     * @throws \Exception
     *
     * create by ck 20190108
     */
    public static function modifyEnumValue($enumId, $name, $value)
    {
        $enum = XhsysEnum::findFirst($enumId);
        if (!$enum) {
            throw new \Exception('该枚举不存在，无法编辑');
        }
        $enum->sEnumName  = $name;
        $enum->sEnumValue = $value;
        if (!$enum->save()) {
            throw new \Exception($enum->getError());
        }
    }

    /**
     * 删除枚举
     *
     * @param $ids
     * @return bool
     *
     * create by ck 20190108
     */
    public static function delEnumValue($ids)
    {
        return XhsysEnum::deleteRecord('iEnumId', $ids);
    }
}
